<template>
  <div class="about">

    <h1>修改分类</h1>

    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="序号">
        <el-input v-model="model.id" readOnly></el-input>
      </el-form-item>

      <el-form-item label="行业名称">
        <el-input v-model="model.name"></el-input>
      </el-form-item>

      <el-form-item label="icon">
          <img :src="model.icon"  min-width="40" height="40" />
      </el-form-item>

      <el-form-item label="点击修改icon">
        <el-upload
                class="avatar-uploader"
                action="http://localhost:8081/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


      <el-form-item>
        <el-select v-model="model.status" placeholder="状态">
          <el-option label="正常" value=0></el-option>
          <el-option label="热门" value=1></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>

<!--    <div>-->
<!--      this is the news page.the transform param is {{this.$route.query.id}}-->
<!--    </div>-->

  </div>
</template>

<script>
  import axios from "axios";

  export default {

    data(){
      return{
        model:{
          id: -1,
          name: 'sec',
          icon: 'www.baidu.com',
          status: 0

        },
        imageUrl: ''
      }
    },

    created() {
      let id = this.$route.query.id
      this.model.id = id
      let _this = this
      axios.get('http://localhost:8081/findAllById/'+id).then(function (response) {
        _this.model = response.data

      })

    },

    methods:{
      save(){
        const _this = this

            axios.post('http://localhost:8081/save',this.model).then(function(resp){
              if(resp.data == 'success'){
                _this.$alert('《'+_this.model.name+'》添加成功！', '消息', {
                  confirmButtonText: '确定',
                })
                _this.$router.push('/')
              }
            })

      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.model.icon = "http://127.0.0.1:8080/image/"+res.body;
        console(res.body);
      },
      beforeAvatarUpload(file) {
        // const isJPG = file.type === 'image/jpeg';
        const isLt5M = file.size / 1024 / 1024 < 5;

        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt5M) {
          this.$message.error('上传头像图片大小不能超过 5MB!');
        }
        // return isJPG && isLt2M;
        return isLt5M;
      }
    }

  }
</script>

<style scoped>
  /*图片上传样式*/
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>